package cartagows.wsframework.wscoordination;

import java.util.HashMap;

import cartago.Artifact;
import cartago.GUARD;
import cartago.OPERATION;
import cartago.OpFeedbackParam;

public class ActivitiesRegistry extends Artifact{

	HashMap<String,ActivityInfo> hashMap;
	
	@OPERATION void init(){
		hashMap = new HashMap<String,ActivityInfo>();
	}

	/**
	 * Operation used for add a new activity in the registry
	 * @param activityId
	 */
	@OPERATION(guard="checkActivityAbsence") void registerNewActivity(String activityId){
		hashMap.put(activityId, new ActivityInfo(activityId));
	}
	
	/**
	 * Operation used for remove an activity from the registry
	 * @param activityId
	 */
	@OPERATION(guard="checkActivityPresenceGuard") void removeActivity(String activityId){
		hashMap.remove(activityId);
	}
	
	/**
	 * Operation used for register a coordinator for a given activity in the registry
	 * @param activityId
	 * @param coordinatorEPR
	 */
	@OPERATION(guard="checkActivityPresenceGuard") void registerCoordinatorForActivity(String activityId, String coordinatorEPR){
		hashMap.get(activityId).setCoordinatorEPR(coordinatorEPR);
	}
	
	/**
	 * Operation used for register a participant for the activity in the registry
	 * @param activityId
	 * @param participantEPR
	 * @param coordinationProtocol
	 */
	@OPERATION (guard="checkActivityAndParticipant")void registerParticipantForActivity(String activityId,String participantEPR,String coordinationProtocol){
		hashMap.get(activityId).getParticipantEPRList().put(participantEPR, coordinationProtocol);
	}
	
	/**
	 * Operation used for check the presence of a given activity
	 * @param activityId
	 */
	@OPERATION void checkActivityPresence(String activityId, OpFeedbackParam<Boolean> res){
		if(hashMap.containsKey(activityId)) res.set(true);
		else res.set(false);
	}

	/**
	 * Operation used for the retrieval of the coordinator for an activity
	 * @param activityId
	 */
	@OPERATION (guard="checkActivityPresenceGuard")void getActivityCoordinatorEPR(String activityId, OpFeedbackParam<String> res){
		res.set(hashMap.get(activityId).getCoordinatorEPR());
	}
	
	/**
	 * Operation that return the participants list for an activity
	 * @param activityId
	 */
	@OPERATION (guard="checkActivityPresenceGuard")void getActivityParticipantList(String activityId, OpFeedbackParam<HashMap<String, String>> res){
		res.set(hashMap.get(activityId).getParticipantEPRList());
	}
	
	/**
	 * Operation used for retrieve the initiator for an activity
	 * @param activityId
	 */
	@OPERATION (guard="checkActivityPresenceGuard") void getInitiatorEPR(String activityId, OpFeedbackParam<String> res){
		res.set(hashMap.get(activityId).getInitiatorEPR());
	}
	
	/**
	 * Operation used for set the initiator for an activity
	 * @param activityId
	 * @param initiatorEPR
	 */
	@OPERATION(guard="checkActivityAndParticipant2") void setInitiator(String activityId,String initiatorEPR){
		hashMap.get(activityId).getParticipantEPRList().remove(initiatorEPR);
		hashMap.get(activityId).setInitiatorEPR(initiatorEPR);
	}

	/**
	 * Operation used for notify the completion of an activity
	 * @param activityId
	 */
	@OPERATION(guard="checkActivityPresenceGuard") void notyfyActivityCompletion(String activityId){
		hashMap.remove(activityId);
	}
	
	/**
	 * 
	 * @param activityId
	 * @param participantEPR
	 * @param coordinationProtocol
	 * @return
	 */
	@GUARD boolean checkActivityAndParticipant(String activityId,String participantEPR,String coordinationProtocol){
		return ((hashMap.containsKey(activityId)) && (!hashMap.get(activityId).getParticipantEPRList().containsKey(participantEPR)));
	}
	
	/**
	 * 
	 * @param activityId
	 * @param initiatorEPR
	 * @return
	 */
	@GUARD boolean checkActivityAndParticipant2(String activityId,String initiatorEPR){
		return ((hashMap.containsKey(activityId)) && (hashMap.get(activityId).getParticipantEPRList().containsKey(initiatorEPR)));
	}
	
	/**
	 * 
	 * @param activityId
	 * @return
	 */
	@GUARD boolean checkActivityPresenceGuard(String activityId){
		return hashMap.containsKey(activityId);
	}

	/**
	 * 
	 * @param activityId
	 * @param coordinatorEPR
	 * @return
	 */
	@GUARD boolean checkActivityPresenceGuard(String activityId, String coordinatorEPR){
		return this.checkActivityPresenceGuard(activityId);
	}

	/**
	 * 
	 * @param activityId
	 * @param res
	 * @return
	 */
	@GUARD boolean checkActivityPresenceGuard(String activityId, OpFeedbackParam<?> res){
		return this.checkActivityPresenceGuard(activityId);
	}

	/**
	 * 
	 * @param activityId
	 * @return
	 */
	@GUARD boolean checkActivityAbsence(String activityId){
		return (!hashMap.containsKey(activityId));
	}
}